package com.objectoriented.tang.utils;

import com.objectoriented.tang.pojo.Chess;

import java.rmi.server.UID;

public class chessboardUtil {
    public static final int CHESSBOARD_SIZE=15;

    public static boolean isWin(int x,int y,int owner,int [][]board){

        int sum=0;
        //X方向是否五子连珠
        for (int i = x+1; i <= CHESSBOARD_SIZE; i++) {
            if (board[i][y]==owner) sum++;
            else break;
        }
        for (int i = x-1; i >=1 ; i--) {
            if (board[i][y]==owner) sum++;
            else break;
        }
        if (sum>=4) return true;
        sum=0;
        //Y方向是否五子珠珠
        for (int j = y+1; j <= CHESSBOARD_SIZE; j++) {
            if (board[x][j]==owner)sum++;
            else break;
        }
        for (int j = y-1; j >=1 ; j--) {
            if (board[x][j]==owner)sum++;
            else break;
        }
        if (sum>=4) return true;
        sum=0;
        //左上到右下方向是否五子连珠
        for (int i = x+1, j=y+1; i<=CHESSBOARD_SIZE &&y<=CHESSBOARD_SIZE ; i++,j++) {
            if (board[i][j]==owner)sum++;
            else break;;
        }
        for (int i = x-1,j=y-1; i >=1&&j>=1; i--,j--) {
            if (board[i][j]==owner)sum++;
            else break;
        }
        if (sum>=4)return true;
        sum=0;
        //左下到右上向向是否五子连珠
        for (int i = x-1,j=y+1; i >=1&&j<=CHESSBOARD_SIZE; i--,j++) {
            if (board[i][j]==owner)sum++;
            else break;
        }
        for (int i = x+1,j=y-1; i <=CHESSBOARD_SIZE&&j>=1 ; i++,j--) {
            if (board[i][j]==owner)sum++;
            else break;
        }
        if (sum>=4)return true;
        return false;
    }

    public static int[] scoringAlgorithm(int [][] chessOwner,int UID) {
        //初始化分数数组
        int [][] chessScore=new int[CHESSBOARD_SIZE+1][CHESSBOARD_SIZE+1];
        for (int i = 1; i <=CHESSBOARD_SIZE ; i++) {
            for (int j = 1; j <=CHESSBOARD_SIZE ; j++) {
                chessScore[i][j]=0;
            }
        }
        //定义变量
        int computerNum=0,humanNum=0,tempScore=0,goalX=0,goalY=0,maxScore=0;
        //扫描横向五元组
        for (int y = 1; y <=CHESSBOARD_SIZE ; y++) {
            for (int x = 1; x <=CHESSBOARD_SIZE-4 ; x++) {
                int k=x;
                while (k<x+5){
                    if (chessOwner[k][y]==UID)humanNum++;
                    else if (chessOwner[k][y]==2)computerNum++;
                    k++;
                }
                tempScore=getScore(computerNum,humanNum);//求出该五元组的分数
                for ( k = x; k<x+5 ; k++) {//为该五元组的每个坐标添加分数
                    chessScore[k][y]+=tempScore;
                }
                humanNum=computerNum=0;
            }
        }
        //扫描纵向五元组
        for (int x = 1; x <=CHESSBOARD_SIZE ; x++) {
            for (int y = 1; y <=CHESSBOARD_SIZE-4 ; y++) {
                int k=y;
                while (k<y+5){
                    if (chessOwner[x][k]==UID)humanNum++;
                    else if (chessOwner[x][k]==2)computerNum++;
                    k++;
                }
                tempScore=getScore(computerNum,humanNum);//求出该五元组的分数
                for ( k = y; k<y+5 ; k++) {//为该五元组的每个坐标添加分数
                    chessScore[x][k]+=tempScore;
                }
                humanNum=computerNum=0;
            }
        }
        //扫描中间到左上角部分
        for (int x = CHESSBOARD_SIZE; x >= 5; x--) {
            for (int y = x-4; y >=1; y--) {
                int i=x-y+1,j=y;
                while (j<y+5){
                    if (chessOwner[i][j]==UID)humanNum++;
                    else if (chessOwner[i][j]==2)computerNum++;
                    i--;
                    j++;
                }
                tempScore=getScore(computerNum,humanNum);
                for (i=x-y+1,j=y;j<y+5; j++,i--) {
                    chessScore[i][j]+=tempScore;
                }
                humanNum=computerNum=0;
            }
        }
        //扫描中间到右下角部分
        for (int x = CHESSBOARD_SIZE-4; x >= 2; x--) {
            for (int y = x+4; y <=CHESSBOARD_SIZE; y++) {
                int i=CHESSBOARD_SIZE+x-y,j=y;
                while (j>y-5){
                    if (chessOwner[i][j]==UID)humanNum++;
                    else if (chessOwner[i][j]==2)computerNum++;
                    i++;
                    j--;
                }
                tempScore=getScore(computerNum,humanNum);
                for (i=CHESSBOARD_SIZE+x-y,j=y;j>y-5; j--,i++) {
                    chessScore[i][j]+=tempScore;
                }
                humanNum=computerNum=0;
            }
        }
        //扫描中间到右上角部分
        for (int x = 1; x <= CHESSBOARD_SIZE-4; x++) {
            for (int y = CHESSBOARD_SIZE-x-3; y >=1; y--) {
                int i=y+x-1,j=y;
                while (j<y+5){
                    if (chessOwner[i][j]==UID)humanNum++;
                    else if (chessOwner[i][j]==2)computerNum++;
                    i++;
                    j++;
                }
                tempScore=getScore(computerNum,humanNum);
                for (i=y+x-1,j=y;j<y+5; j++,i++) {
                    chessScore[i][j]+=tempScore;
                }
                humanNum=computerNum=0;
            }
        }
        //扫描中间到左下角部分
        for (int x = CHESSBOARD_SIZE; x >=5; x--) {
            for (int y = CHESSBOARD_SIZE-x+5; y <=CHESSBOARD_SIZE; y++) {
                int i=y+x-CHESSBOARD_SIZE,j=y;
                while (j>y-5){
                    if (chessOwner[i][j]==UID)humanNum++;
                    else if (chessOwner[i][j]==2)computerNum++;
                    i--;
                    j--;
                }
                tempScore=getScore(computerNum,humanNum);
                for (i=y+x-CHESSBOARD_SIZE,j=y;j>y-5; j--,i--) {
                    chessScore[i][j]+=tempScore;
                }
                humanNum=computerNum=0;
            }
        }
        //求最优位置
        for (int i = 1; i <=CHESSBOARD_SIZE ; i++) {
            for (int j = 1; j <=CHESSBOARD_SIZE ; j++) {
                if (chessOwner[i][j]==0&&chessScore[i][j]>maxScore){
                    maxScore=chessScore[i][j];
                    goalX=i;
                    goalY=j;
                }
            }
        }
        int [] result={goalX,goalY};
        return  result;
    }
    private static int getScore(int computerNum, int humanNum) {
        if (computerNum>0&&humanNum>0)return 0;
        if (computerNum==0&&humanNum==0)return 7;
        if (computerNum==1)return 35;
        if (computerNum==2)return 800;
        if (computerNum==3)return 15000;
        if (computerNum==4)return 800000;
        if (humanNum==1)return 15;
        if (humanNum==2)return 400;
        if (humanNum==3)return 1800;
        if (humanNum==4)return 100000;
        return 0;
    }

}
